package com.bid.base.common.util;

import java.util.List;
/**
 * <strong>Description : </strong> <br>
 * <p>
 * <strong>北京正源德标科技信息有限责任公司</strong> <br>
 * </p>
 * <br>
 *
 * @author : zhaots
 * @date Date : 2020年12月07日 18:22
 * 修改人 修改日期 修改描述<br>
 * -------------------------------------------<br>
 * <br>
 * <br>
 */
public class Paging<T> {
	
	//每页显示的数量
	private int countPerPage;
	//总记录数
	private int totalItem;
	//当前页面
	private int currentPageIndex;
	//页面总数
	private int pageCount;
	//页面显示的页码数量(这里默认每页显示5条)
	private int everyPageNum =5;
	//每页的起始编号
	private int pageStartRow;
	private int beginPageIndex;
	private int endPageIndex;
	private List<T> list;
	/////////////////////////////分页数据组装
	private int first;// 首页索引
	private int last;// 尾页索引
	private int prev;// 上一页索引
	private int next;// 下一页索引
	//private int pageNo = 1; // 当前页码
	private int pageNo =currentPageIndex;
	private int pageSize =everyPageNum;
	private boolean firstPage;//是否是第一页
	private boolean lastPage;//是否是最后一页
	private int length = 8;// 显示页面长度
	private int slider = 1;// 前后显示页面长度
	private String funcName = "page"; // 设置点击页码调用的js函数名称，默认为page，在一页有多个分页对象时使用。
	private String message = ""; // 设置提示消息，显示在“共n条”之后
	private long count=totalItem;// 总记录数，设置为“-1”表示不查询总数
	public void initialize(){
		this.first = 1;
		//logs.debug("1 paging>>>>>>>>>>last:\t"+last);
		this.last = (int)(count / (this.pageSize < 1 ? 20 : this.pageSize) + first - 1);
		//logs.debug("2 paging>>>>>>>>>>last:\t"+last);
		if (this.count % this.pageSize != 0 || this.last == 0) {
			this.last++;
		}
		//logs.debug("3 paging>>>>>>>>>>last:\t"+last);
		if (this.last < this.first) {
			this.last = this.first;
		}
		if (this.pageNo <= 1) {
			this.pageNo = this.first;
			this.firstPage=true;
		}
		if (this.pageNo >= this.last) {
			this.pageNo = this.last;
			this.lastPage=true;
		}
		if (this.pageNo < this.last - 1) {
			this.next = this.pageNo + 1;
		} else {
			this.next = this.last;
		}
		if (this.pageNo > 1) {
			this.prev = this.pageNo - 1;
		} else {
			this.prev = this.first;
		}
		//2
		if (this.pageNo < this.first) {// 如果当前页小于首页
			this.pageNo = this.first;
		}

		if (this.pageNo > this.last) {// 如果当前页大于尾页
			this.pageNo = this.last;
		}
		
	}
	/**
	 * 默认输出当前分页标签 
	 * <div class="page">${page}</div>
	 */
	@Override
	public String toString() {
		return getString("");
		
	}
	public String toString4at() {
		return getString("4at");
	}
	private String getString(String appendStr) {
		
		String funcName= this.funcName+appendStr;
		initialize();
		StringBuilder sb = new StringBuilder();
		if (pageNo == first) {// 如果是首页
			sb.append("<li class=\"disabled\"><a href=\"javascript:\">&#171; 上一页</a></li>\n");
		} else {
			sb.append("<li><a onclick=\"javascript:"+funcName+"("+prev+","+pageSize+",this);\">&#171; 上一页</a></li>\n");
		}
		int begin = pageNo - (length / 2);
		//logs.debug("4 paging>>>>>>>>>>begin:\t"+begin+"\t first:\t"+first);
		if (begin < first) {
			begin = first;
		}
		//logs.debug("5 paging>>>>>>>>>>begin:\t"+begin+"\t first:\t"+first);
		int end = begin + length - 1;
		if (end >= last) {
			end = last;
			begin = end - length + 1;
			if (begin < first) {
				begin = first;
			}
		}
		if (begin > first) {
			int i = 0;
			for (i = first; i < first + slider && i < begin; i++) {
				sb.append("<li><a onclick=\"javascript:"+funcName+"("+i+","+pageSize+",this);\">"
						+ (i + 1 - first) + "</a></li>\n");
			}
			if (i < begin) {
				sb.append("<li class=\"disabled\"><a href=\"javascript:\">...</a></li>\n");
			}
		}
		for (int i = begin; i <= end; i++) {
			if (i == pageNo) {
				sb.append("<li class=\"active\"><a href=\"javascript:\">" + (i + 1 - first)
						+ "</a></li>\n");
			} else {
				sb.append("<li><a onclick=\"javascript:"+funcName+"("+i+","+pageSize+",this);\">"
						+ (i + 1 - first) + "</a></li>\n");
			}
		}
		if (last - end > slider) {
			sb.append("<li class=\"disabled\"><a href=\"javascript:\">...</a></li>\n");
			end = last - slider;
		}
		for (int i = end + 1; i <= last; i++) {
			sb.append("<li><a onclick=\"javascript:"+funcName+"("+i+","+pageSize+",this);\">"
					+ (i + 1 - first) + "</a></li>\n");
		}
		if (pageNo == last) {
			sb.append("<li class=\"disabled\"><a href=\"javascript:\">下一页 &#187;</a></li>\n");
		} else {
			sb.append("<li><a onclick=\"javascript:"+funcName+"("+next+","+pageSize+",this);\">"
					+ "下一页 &#187;</a></li>\n");
		}
		sb.append("<li class=\"disabled controls\"><a href=\"javascript:\">当前 ");
		sb.append("<input type=\"text\" value=\""+pageNo+"\" onkeypress=\"var e=window.event||this;var c=e.keyCode||e.which;if(c==13)");
		sb.append(funcName+"(this.value,"+pageSize+",this);\" onclick=\"this.select();\"/> / ");
		//sb.append("<input type=\"text\" value=\""+pageSize+"\" onkeypress=\"var e=window.event||this;var c=e.keyCode||e.which;if(c==13)");
		int p= (int) Math.ceil(count*1.0/pageSize);
		sb.append("<input type=\"text\" value=\""+(p==0?1:p)+"\" onkeypress=\"var e=window.event||this;var c=e.keyCode||e.which;if(c==13)");
		sb.append(funcName+"("+pageNo+",this.value,this);\" onclick=\"this.select();\"/> 页，");
		sb.append("共 " + count + " 条"+(message!=null?message:"")+"</a><li>\n");
		sb.insert(0,"<ul>\n").append("</ul>\n");
		sb.append("<div style=\"clear:both;\"></div>");
		return sb.toString();
	}
	
	
	/**
	 * 获取设置总数
	 * @return
	 */
	public long getCount() {
		return count;
	}

	/**
	 * 设置数据总数
	 * @param count
	 */
	public void setCount(long count) {
		this.count = count;
		if (pageSize >= count){
			pageNo = 1;
		}
	}
	
	/**
	 * 获取当前页码
	 * @return
	 */
	public int getPageNo() {
		return pageNo;
	}
	
	/**
	 * 设置当前页码
	 * @param pageNo
	 */
	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}
	
	/**
	 * 获取页面大小
	 * @return
	 */
	public int getPageSize() {
		return pageSize;
	}

	/**
	 * 设置页面大小（最大500）
	 * @param pageSize
	 */
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize <= 0 ? 10 : pageSize > 500 ? 500 : pageSize;
	}

	/**
	 * 首页索引
	 * @return
	 */
	public int getFirst() {
		return first;
	}

	/**
	 * 尾页索引
	 * @return
	 */
	public int getLast() {
		return last;
	}
	
	/**
	 * 获取页面总数
	 * @return getLast();
	 */
	public int getTotalPage() {
		return getLast();
	}

	/**
	 * 是否为第一页
	 * @return
	 */
	public boolean isFirstPage() {
		return firstPage;
	}

	/**
	 * 是否为最后一页
	 * @return
	 */
	public boolean isLastPage() {
		return lastPage;
	}
	/**
	 * 上一页索引值
	 * @return
	 */
	public int getPrev() {
		if (isFirstPage()) {
			return pageNo;
		} else {
			return pageNo - 1;
		}
	}
	/**
	 * 下一页索引值
	 * @return
	 */
	public int getNext() {
		if (isLastPage()) {
			return pageNo;
		} else {
			return pageNo + 1;
		}
	}
	/**
	 * 获取点击页码调用的js函数名称
	 * function ${page.funcName}(pageNo){location="${ctx}/list-${category.id}${urlSuffix}?pageNo="+i;}
	 * @return
	 */
	public String getFuncName() {
		return funcName;
	}

	/**
	 * 设置点击页码调用的js函数名称，默认为page，在一页有多个分页对象时使用。
	 * @param funcName 默认为page
	 */
	public void setFuncName(String funcName) {
		this.funcName = funcName;
	}

	/**
	 * 设置提示消息，显示在“共n条”之后
	 * @param message
	 */
	public void setMessage(String message) {
		this.message = message;
	}
	
	/**
	 * 分页是否有效
	 * @return this.pageSize==-1
	 */
	public boolean isDisabled() {
		return this.pageSize==-1;
	}
	
	/**
	 * 是否进行总数统计
	 * @return this.count==-1
	 */
	public boolean isNotCount() {
		return this.count==-1;
	}
	
	/**
	 * 获取 Hibernate FirstResult
	 */
	public int getFirstResult(){
		int firstResult = (getPageNo() - 1) * getPageSize();
		if (firstResult >= getCount()) {
			firstResult = 0;
		}
		return firstResult;
	}
	/**
	 * 获取 Hibernate MaxResults
	 */
	public int getMaxResults(){
		return getPageSize();
	}
	//////////////////////////////
	public Paging() {
	}
	
	public Paging(String countPerPage, int totalItem, String currentPageIndex) {
		this.countPerPage = countPerPage==null?everyPageNum:Integer.parseInt(countPerPage);
		this.totalItem = totalItem;
		this.currentPageIndex = currentPageIndex == null ? 1:(currentPageIndex == "" ? 1 : Integer.parseInt(currentPageIndex));
		this.pageCount = (int) Math.ceil(totalItem*1.0/this.countPerPage);
		this.pageStartRow = (this.currentPageIndex-1)*this.countPerPage;
		this.beginPageIndex = this.currentPageIndex-everyPageNum/2>0?this.currentPageIndex-everyPageNum/2:1;
		this.endPageIndex = (this.currentPageIndex +everyPageNum/2-1) > pageCount ? pageCount:(this.currentPageIndex +everyPageNum/2-1);
		//this.pageSize=this.everyPageNum;//分页数据组装
		this.pageSize=this.countPerPage;
		this.pageNo =this.currentPageIndex;
		this.count=totalItem;
	}
	public int getCountPerPage() {
		return countPerPage;
	}
	public void setCountPerPage(int countPerPage) {
		this.countPerPage = countPerPage;
	}
	public int getTotalItem() {
		return totalItem;
	}
	public void setTotalItem(int totalItem) {
		this.totalItem = totalItem;
	}
	public int getCurrentPageIndex() {
		return currentPageIndex;
	}
	public void setCurrentPageIndex(int currentPageIndex) {
		this.currentPageIndex = currentPageIndex;
	}
	public int getPageCount() {
		return pageCount;
	}
	public void setPageCount(int pageCount) {
		this.pageCount = pageCount;
	}
	public int getEveryPageNum() {
		return everyPageNum;
	}
	public void setEveryPageNum(int everyPageNum) {
		this.everyPageNum = everyPageNum;
	}
	public int getPageStartRow() {
		return pageStartRow;
	}
	public void setPageStartRow(int pageStartRow) {
		this.pageStartRow = pageStartRow;
	}
	public int getBeginPageIndex() {
		return beginPageIndex;
	}
	public void setBeginPageIndex(int beginPageIndex) {
		this.beginPageIndex = beginPageIndex;
	}
	public int getEndPageIndex() {
		return endPageIndex;
	}
	public void setEndPageIndex(int endPageIndex) {
		this.endPageIndex = endPageIndex;
	}
	public List<T> getList() {
		return list;
	}
	public void setList(List<T> list) {
		this.list = list;
	}

}
